package com.ailiwu.www.ailiwu.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/6 000611:29
 * Description:用来写带toolbar的Webview的activity
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/6 0006       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class BaseWebViewActivity extends BaseActivity {

    private WebView mWebView;
    private Toolbar toolbar;
    private CollapsingToolbarLayout collapsingToolbar;
    private ImageView imageView;
    private TextView tv_title;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;

    @Override
    protected void loadXml() {
        setContentView(R.layout.baseactivity_webview);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {

    }

    @Override
    protected void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        imageView = (ImageView) findViewById(R.id.backdrop);
        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(false);
        tv_title = (TextView) findViewById(R.id.title);

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        options = new DisplayImageOptions.Builder()
                .showImageOnFail(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
                .showImageOnLoading(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
//                .delayBeforeLoading(R.drawable.com_taobao_tae_sdk_simple_toast_bg)
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(6))   // 设置成圆角图片
                .build();
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String iamge_url = intent.getStringExtra("iamge_url");
        String content_url = intent.getStringExtra("content_url");
        String title = intent.getStringExtra("title");

        collapsingToolbar.setTitle(title);
        tv_title.setText(title);
        imageLoader.displayImage(iamge_url, imageView, options);
        mWebView.loadUrl(content_url);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
